<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2019/08/05 14:55
 * Desc: desc
 */
class Model_Api_Standard_Travelnotes
{


    private static $_typeid = 101;

    /**
     * @desc 游记列表
     * @param $params
     * @return array
     */
    public static function search($params)
    {
        $status = true;
        $page = intval($params['page']);
        $page = $page ? $page : 1;
        $pagesize = intval($params['pagesize']);
        $pagesize = $pagesize ? $pagesize : 10;
        $keyword = St_String::filter_mark(Common::remove_xss($params['keyword']));
        $ordertable = isset($params['destid']) ? 'sline_kindorderlist' : 'sline_allorderlist' ;

        $value_arr = array();
        $where = " WHERE a.status=1 ";

        //按关键词
        if (!empty($keyword))
        {
            $value_arr[':keyword'] = '%' . $keyword . '%';
            $where .= " AND a.title like :keyword ";
        }

        $offset = (intval($page) - 1) * $pagesize;

        $sql = "SELECT a.*,c.litpic as userImg FROM `sline_notes` a ";
        $sql .= "LEFT JOIN `{$ordertable}` b ";
        $sql .= "ON (a.id=b.aid and b.typeid=101) ";
        $sql .= "LEFT JOIN `sline_member` c ";
        $sql .= "ON (a.memberid=c.mid) ";
        $sql .= $where;

        //计算总数
        $totalSql = "SELECT count(*) as dd " . strchr($sql, " FROM");
        $totalSql = str_replace(strchr($totalSql, "ORDER BY"), '', $totalSql);//去掉order by

        $totalN = DB::query(1, $totalSql)->parameters($value_arr)->execute()->as_array();
        $totalNum = $totalN[0]['dd'] ? $totalN[0]['dd'] : 0;

        //数据量大时的优化方法,数据量小时不推荐使用此方法
        //$idWhere = "SELECT id FROM `sline_line` ORDER BY id limit $offset, 1";
        //$sql = str_replace("WHERE","WHERE a.id>($idWhere) AND",$sql);
        //$sql.= "LIMIT {$pagesize}";

        //检测页面数量是否超限
        $total_page = (int)ceil($totalNum / $pagesize);
        if (($total_page > 0 AND intval($page) >= $total_page) OR ($total_page == 0 AND $page > 1))
        {
            $status = false;
        }

        $sql .= " ORDER BY a.modtime DESC,a.addtime DESC,IFNULL(b.displayorder,9999) ASC";
        $sql .= " LIMIT {$offset},{$pagesize}";

        $arr = DB::query(1, $sql)->parameters($value_arr)->execute()->as_array();
        foreach ($arr as &$v)
        {
            $v['modtime']  = date("Y-m-d",$v['modtime']);
            $v['favorite'] = $v['favorite'] ? $v['favorite'] : 0;
            $v['favorite'] += $v['virtual_favorite'];
            $v['shownum']  = $v['shownum'] ? $v['shownum'] : 0;
            $v['shownum']  += $v['read_num'];
            // 封面图片
            if(empty($v['litpic']))
            {
                $v['litpic']=$v['bannerpic'];
            }
            $v['litpic'] = Common::img($v['litpic']);
        }
        $out = array(
            'row_count' => $totalNum,
            'list'      => $arr,
            'status'    => $status
        );
        return $out;

    }

    /**
     * @desc 详情
     * @param $id
     * @return array
     */
    public static function detail($params)
    {
        if($params['id'])
        {
            $id   = $params['id'];
            $info = DB::select()->from('notes')->where('id','=',$id)->execute()->current();
            // 处理封面
            if(empty($info['litpic']))
            {
                $info['litpic'] = $info['bannerpic'];
            }
            $info['litpic'] = Common::img($info['litpic']);
            $info['modtime'] = date("Y-m-d",$info['modtime']);
            return $info;
        }
        else
        {
            return array();
        }

    }


    // 给html中添加域名
    public static function add_http_html($content,$tag)
    {
        $contentArr = explode($tag,$content);
        $newContent = $contentArr[0];
        foreach ($contentArr as $k=>$item)
        {
            if($k > 0)
            {
                $newContent .= $tag;//'<img src="';
                // 判断前四个单词是否是http
                $prvstr = substr($item,0,4);
                $ext = '';
                if($prvstr != 'http')
                {
                    $ext = $GLOBALS['cfg_basehost'];
                }

                $newContent .= $ext.$item;
            }
        }

        return $newContent;
    }

    /**
     * 点赞记录
     * @function
     * @param $hash_key
     * @param $type
     * @return bool
     */
    public static function favorite_log($hash_key,$type = 'get')
    {
        if($type == 'add')
        { // 添加
            $data = array(
                'hash_key'=> $hash_key,
                'addtime'=> time()
            );
            DB::insert('notes_favorite_log',array_keys($data))->values(array_values($data))->execute();
            return true;
        }
        else
        { // 验证
            $res = DB::select()->from("notes_favorite_log")->where("hash_key","=",$hash_key)->execute()->current();

            return $res ? true : false;
        }
    }

}